<%@ page language="java" contentType="text/html; charset=UTF-8"   pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@ include file="international.jsp" %> 
<jsp:useBean id="userService" class="com.brother.service.UserService"></jsp:useBean>

<jsp:useBean id="postUser" class="com.brother.model.Customer"></jsp:useBean>
<jsp:setProperty property="*" name="postUser"/>
<%
com.brother.model.Customer user = (com.brother.model.Customer)session.getAttribute(com.brother.Constants.LOGIN_SESSTION_NAME);
if(user == null || user.getUserid() == 0 || !user.isAdmin())
	return;
String userid = request.getParameter("userid") == null ?"":request.getParameter("userid").trim();
String action = request.getParameter("action");

boolean alreadyExistUser = false;


if(action != null && action.equalsIgnoreCase("submitted")){
	
	boolean actionSuc = false;

	alreadyExistUser = (userService.GetUserByUserFieldName("name", postUser.getName()) != null);
	
	if(postUser.getName() != null && postUser.getName().length() != 0 && !alreadyExistUser){ // 添加user

		String parentId = (user.getParentid()==null ? "":(user.getParentid()+","))+user.getUserid();
		//System.out.println("000:"+ parentId);
		postUser.setParentid(parentId);
		//postUser.setAdmin(false);
		userService.normalAddUser(postUser);
			
		actionSuc = true;
		
	}else{  //  修改tracker
		userService.normalUpdateUser(postUser);
		actionSuc = true;
	}
	if(actionSuc)
		response.sendRedirect("userList.jsp");
}else{
	if(userid!= null && !userid.isEmpty() && userid.trim().matches("^\\d+$")){
		postUser = userService.GetUserByUserId(Integer.parseInt(userid));
	}
}


%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title><%if(userid.length() == 0){ %>
		<fmt:message key="manage.user.add"/>
		<%}else{ %>
		<fmt:message key="manage.user.modify"/>
		<%} %> - <fmt:message key="company.name"/></title>
<meta name="description" content="" />
<link rel="stylesheet" type="text/css" media="all" href="style.css" />
<link rel="stylesheet" type="text/css" href="css/ext-all.css" />
<link rel="stylesheet" type="text/css" href="css/grid-examples.css" />

    <script type="text/javascript" src="js/ext-base.js"></script>
    <script type="text/javascript" src="js/ext-all.js"></script>

    
  <script type='text/javascript' src='dwr/engine.js'> </script>
  <script type='text/javascript' src='dwr/util.js'> </script>
  <script type='text/javascript' src='dwr/interface/TrackerService.js'> </script>
    <script type='text/javascript' src='dwr/interface/UserService.js'> </script>
    
    
    
  <style type="text/css">
<!--
.STYLE1 {color: #FFFFFF}
.STYLE2 {color: #FF0000}
body{
background-color: #FFFFFF;
}
-->
  </style>
</head>
<body >
<script>

<%if(userid.length() == 0){ %>
var edit_user_title = '<fmt:message key="manage.user.add"/>';
<%}else{ %>
var edit_user_title = '<fmt:message key="manage.user.modify"/>';
<%} %> 

Ext.onReady(function(){
	document.getElementById('password').value = '';
	Ext.MessageBox.buttonText.yes = '<fmt:message key="button.yes.text"/>';
	Ext.MessageBox.buttonText.no = '<fmt:message key="button.no.text"/>';
	Ext.MessageBox.buttonText.cancel = '<fmt:message key="button.cancel.text"/>';
	
	top.ajustProducts(edit_user_title, 590, 630);
	
});



var UserIDRequired = '<fmt:message key="user.userid.required"/>';
var PasswordRequired = '<fmt:message key="user.password.required"/>';
var overwritePassword = '<fmt:message key="user.password.overwrite.warning"/>';

function checkSubmit(){
	
	var userID = document.getElementById("userAutocomplete").value;
	if(userID.indexOf("Select") > -1){
		userID = "";
	}
	if(userID.length == 0){
		//alert(UserIDRequired);
		Ext.MessageBox.show({
            title: '',
            msg: UserIDRequired,
            buttons: Ext.MessageBox.OK,
            icon: Ext.MessageBox.WARNING
        });
		return false;
	}
	var password = document.getElementById("password").value;
	if(password.length == 0){
		//alert(PasswordRequired);
		//return false;
	}else if(document.getElementById("userid").value != "0" && !confirm(overwritePassword)){
		document.getElementById("password").focus();
		return false;
	}

	if(isExistsUserID){
		//alert("<fmt:message key="user.id.exists"/>");
		Ext.MessageBox.show({
            title: '',
            msg: "<fmt:message key="user.id.exists"/>",
            buttons: Ext.MessageBox.OK,
            icon: Ext.MessageBox.WARNING
        });
		return false;
	}
	
	return true;
}

function onUserSelect(user){
	document.getElementById("userid").value = user.userid;
	onUserInput(user.name);
}

function onUserInput(name){
	UserService.GetUserByUserFieldName('name',name, function(model){
		if(model != null){
			isExistsUserID = true;
		}else{
			isExistsUserID = false;
		}
	});
}


var isExistsUserID = false;
function checkUserExist(userid){
	UserService.GetUserByUserId(userid, function(model){
		if(model != null){
			isExistsUserID = true;
		}else{
			isExistsUserID = false;
		}
	});
}
</script>



<table width="100%" align="left" >
	<tr >

	
		<td valign="top" align="left" >
		
		<form action="edituser.jsp" style="width:570px;height:520px" method="post" onSubmit="return checkSubmit();">

						<table align="left"  class="tableBorder1" cellpadding="1" border="0" cellspacing="0" width="100%">
				  
				  <%if(alreadyExistUser){ %>
				  
				  <tr>
				  <td colspan="2"><SPAN style="color:red;"><fmt:message key="user.already.exist"/></SPAN></td>
				</tr>
				  <%} %>
			
				<tr>
				  <th align="left" style="border-right: 0px !important;"><fmt:message key="user.id"/>: </th>
				  <td nowrap="nowrap" style="border-left: 0px;"><input onblur="onUserInput(this.value)" <%if(userid.length() > 0){ %>readonly="true"<%} %> name="name" onChange="onUserInput(this.value)" type="text" value="<%=postUser.getName()==null?(""):postUser.getName()%>" id="userAutocomplete" size="30">
			      <span class="STYLE2">*</span></td>
				  </tr>
				<tr>
				  <th align="right" style="border-right: 0px !important;"><fmt:message key="user.password"/>:</th>
				  <td><input name="password" type="password" <%=(postUser.getPassword()==null)?"":"value=\""+postUser.getPassword()+"\""%> id="password" size="30">
			      <span class="STYLE2">*</span></td>
				  </tr>
				<tr>
				  <th align="right" style="border-right: 0px !important;"><fmt:message key="user.username"/>: </th>
				  <td><input name="realName" type="text" value="<%=postUser.getRealName()==null?"":postUser.getRealName()%>" id="realName" size="30"></td>
				  </tr>
				 
				 <%if(user.isAdmin()){ %> 
				 <tr>
				  <th align="right" style="border-right: 0px !important;"><fmt:message key="user.set.admin"/>: </th>
				  <td><input name="admin" <%if(postUser.isAdmin()){ %> checked="checked"<%} %> type="checkbox" value="true" id="admin"/></td>
				  </tr> 
				  <%} %>
				  
				<tr>
				  <th align="right" style="border-right: 0px !important;"><fmt:message key="user.gender"/>:</th>
				  <td><input type="radio" id="maleGender" name="gender" <%=postUser.getGender()==null?"":("Male".equals(postUser.getGender())? "checked":"")%> value="Male">
				     <fmt:message key="user.gender.male"/>
			          <input name="gender" id="femaleGender" type="radio" value="Female" <%=postUser.getGender()==null?"checked":("Female".equals(postUser.getGender())? "checked":"")%>>
		       <fmt:message key="user.gender.female"/></td>
				  </tr>
				<tr>
				  <th align="right" style="border-right: 0px !important;"><fmt:message key="user.company.name"/>: </th>
				  <td><input name="companyName" type="text" value="<%=postUser.getCompanyName()==null?"":postUser.getCompanyName()%>" id="companyName" size="50"></td>
				  </tr>
				<tr>
				  <th align="right" style="border-right: 0px !important;"><fmt:message key="user.email"/>:</th>
				  <td><input name="email" type="text" value="<%=postUser.getEmail()==null?"":postUser.getEmail()%>" id="email" size="50"></td>
				  </tr>
				<tr>
				  <th align="right" style="border-right: 0px !important;"><fmt:message key="user.address"/>:</th>
				  <td><input name="address" type="text" value="<%=postUser.getAddress()==null?"":postUser.getAddress()%>" id="address" size="50"></td>
				  </tr>
				<tr>
				  <th align="right" style="border-right: 0px !important;"><fmt:message key="user.country"/>:</th>
				  <td><input name="country" type="text" value="<%=postUser.getCountry()==null?"":postUser.getCountry()%>" id="country" size="30"></td>
				  </tr>
				<tr>
				  <th align="right" style="border-right: 0px !important;"><fmt:message key="user.mobile"/>:</th>
				  <td><input name="mobile" type="text" id="mobile" value="<%=postUser.getMobile()==null?"":postUser.getMobile()%>" size="30"></td>
				  </tr>
				<tr>
				  <th align="right" style="border-right: 0px !important;"><fmt:message key="user.fax"/>:</th>
				  <td><input name="fax" value="<%=postUser.getFax()==null?"":postUser.getFax()%>" type="text" id="fax" size="30"></td>
				  </tr>
				<tr>
				  <th align="right" style="border-right: 0px !important;"><fmt:message key="user.phone.number"/>: </th>
				  <td><input name="phoneNumber" type="text" value="<%=postUser.getPhoneNumber()==null?"":postUser.getPhoneNumber()%>" id="phoneNumber" size="30"></td>
				  </tr>
				<tr>
				  <th align="right" style="border-right: 0px !important;"><fmt:message key="user.description"/>:</th>
				  <td>			    <input type="hidden" name="userid" id="userid" value="<%=postUser.getUserid()== 0?"0":postUser.getUserid()%>" />
						<input type="hidden" name="action" value="submitted" />
	<textarea name="description" cols="50" rows="8" id="description"><%=postUser.getDescription()==null?"":postUser.getDescription()%></textarea></td>
				  </tr>
				
			</table>
			
			</form>
		
		</td>
	</tr>
	<tr><td>
	<table width="100%" cellpadding="0" cellspacing="0" border="0"><tr>
					<td width="210">&nbsp;</td>
					<td width="110" align="left" id="submitBtm">
					
							
							</td>
							<td id="cancelBtm" align="left" >
							
							<script>
				Ext.onReady(function(){
					 var button = new Ext.Button({renderTo:'submitBtm',type:'button', 
						text:'<%if(userid.length() == 0){ %><fmt:message key="manage.add.submit"/><%}else{ %><fmt:message key="manage.modify"/><%} %>',split: false,width:100,
						enableToggle:false});

						button.on('click',function(){
							if(!checkSubmit()){
								return;
							}
							document.forms[0].submit();
						});

						button = new Ext.Button({renderTo:'cancelBtm',type:'button', 
							text:'<fmt:message key="form.cancel"/>',split: false,width:100,
							enableToggle:false});

							button.on('click',function(){
								location.href='userList.jsp';
							});
						});

				
				</script>
							</td>
							
				</tr></table>
	</td></tr>
</table>




</body>
</html>
<%
com.brother.database.DataBase.closeThreadConnection();
%>